﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AudioDemo.View
{
    /// <summary>
    /// text3.xaml 的交互逻辑
    /// </summary>
    public partial class text3 : Window
    {
        public text3()
        {
            InitializeComponent();
            richTextBox.Document = doc;
            //添加内容
            appendLine("one", "海上生明月");
            appendLine("two", "张三丰");
        }
        FlowDocument doc = new FlowDocument();
        private void appendLine(string name, string line)
        {
            Paragraph p = new Paragraph();
            doc.RegisterName(name, p);
            Run r = new Run(line);
            p.TextAlignment = TextAlignment.Center;
            p.Inlines.Add(r);
            doc.Blocks.Add(p);
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            //获取段落部分的内容
            Paragraph p = doc.FindName("one") as Paragraph;
            TextRange range = new TextRange(p.ContentStart, p.ContentEnd);
            string str = range.Text;
            MessageBox.Show(str);
            //修改段落内容
            p.Inlines.Clear();
            p.Inlines.Add(new Run("天涯共此时"));
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //获取段落部分的内容
            Paragraph p = doc.FindName("one") as Paragraph;
            TextRange range = new TextRange(p.ContentStart, p.ContentEnd);
            string str = range.Text;
            //修改样式
            p.FontSize = 20;
            p.Inlines.Clear();
            Span s = new Span();
            s.Foreground = Brushes.Red;
            s.Inlines.Add(new Run(str.Substring(0, 1)));
            p.Inlines.Add(s);
            p.Inlines.Add(new Run(str.Substring(1, str.Length - 1)));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //1.设置选中区域(目前测试无效)
            //TextPointer start = richTextBox.GetPositionFromPoint(new Point(0, 0), true);
            //TextPointer end = richTextBox.GetPositionFromPoint(new Point(500,500),true);
            //richTextBox.Selection.Select(start, end);

            //TextSelect继承自TextRange
            TextSelection selection = richTextBox.Selection;
            //1.获取选中内容
            string result = selection.Text;
            MessageBox.Show(result);
        }
    }
}
